package com.supermap.dataservice.cloud.adv;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.cloud.adv.AdcCloudConment;
import com.supermap.dubbo.api.cloud.adv.AdvCloud;
import com.supermap.dubbo.api.cloud.adv.AdvisoryCloudService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2018年11月17 22:36
 * @email 1301584033@qq.com
 */
@Service
public class AdvisoryCloudServiceImpl implements AdvisoryCloudService {
    @Resource
    private AdvisoryCloudMpper advisoryCloudMpper;

    @Override
    public String getAdvIdByAdvCommentId(String commenId) {
        return advisoryCloudMpper.getAdvIdByAdvCommentId(commenId);
    }

    @Override
    public List<Map<String, Object>> getSubject(String userId) {
        return advisoryCloudMpper.getSubject(userId);
    }

    @Override
    public JSONUtil insertAdvisoryCollect(String userId, String advId) {
        int count= advisoryCloudMpper.insertAdvisoryCollect(userId,advId);
        if(count>0){
            return  new JSONUtil();
        }
        return new JSONUtil().setCode(100).setIsSuccess(false).setMsg("收藏失败");
    }

    @Override
    public Map<String, Object> getAdvById(String advId) {
        return advisoryCloudMpper.getAdbById(advId);
    }

    @Override
    public JSONUtil delAdvisoryCollect(String userId, String advId) {
        int count= advisoryCloudMpper.delAdvisoryCollect(userId,advId);
        if(count>0){
            return  new JSONUtil();
        }
        return new JSONUtil().setCode(100).setIsSuccess(false).setMsg("删除收藏失败");
    }

    @Override
    public JSONUtil insertAdvisoryPraise(String userId, String advId) {
        int count= advisoryCloudMpper.insertAdvisoryPraise(userId,advId);
        if(count>0){
            return  new JSONUtil();
        }
        return new JSONUtil().setCode(100).setIsSuccess(false).setMsg("点赞失败");
    }

    @Override
    public JSONUtil delAdvisoryPraise(String userId, String advId) {
        int count= advisoryCloudMpper.delAdvisoryPraise(userId,advId);
        if(count>0){
            return  new JSONUtil();
        }
        return new JSONUtil().setCode(100).setIsSuccess(false).setMsg("删除点赞失败");
    }


        @Override
        public JSONUtil insertAdvisory(AdvCloud advCloud) {
            int advId =0;
            try {
                        int count = advisoryCloudMpper.insertAdvisory(advCloud);
                        advId = advisoryCloudMpper.getInsertAdvisoryId();
                        if (advCloud.getImgs() != null && !advCloud.getImgs().equals("")) {
                            advCloud.setImgs(advCloud.getImgs().replace("[",""));
                            advCloud.setImgs(advCloud.getImgs().replace("]",""));
                            advCloud.setImgs(advCloud.getImgs().replace("\"",""));
                            String str[] = advCloud.getImgs().split(",");

                            for (String s : str) {
                                advisoryCloudMpper.insertAdvisoryImg(advId, s);
                            }
                        }
                    return new JSONUtil().setCountSum(advId);
                }catch (Exception e){
                    e.printStackTrace();
                    return new JSONUtil().setCode(100).setIsSuccess(false).setMsg("添加失败!").setCountSum(advId);
                }


         }



    @Override
    public JSONUtil insertAdvisoryConment(AdcCloudConment adcCloudConment) {
        try {
            int count = advisoryCloudMpper.insertAdvinfocomment(adcCloudConment);
            return new JSONUtil();
        }catch (Exception e){
            e.printStackTrace();
            return new JSONUtil().setCode(100).setIsSuccess(false).setMsg("添加失败!");
        }

    }

    @Override
    public JSONUtil getPageAdvConmment(String advId, Integer pageNum, Integer pageSise) {
        pageNum=(pageNum-1)*pageSise;
            try{
                List<AdvComment> advCommentList = advisoryCloudMpper.
                        getPageAdvConmment(advId,pageNum,pageSise,-1);
                return  new JSONUtil().setData(getListAdvComent(advCommentList));
            }catch (Exception e){
                e.printStackTrace();
                return new JSONUtil().setCode(100).setIsSuccess(false).setMsg("查询失败");
            }
    }


    //递归
    public List<AdvComment> getListAdvComent(List<AdvComment> list){
        List<AdvComment> advCommentList = list;
            for(int i =0;i<advCommentList.size();i++){
                List<AdvComment> advCommentListzi = advisoryCloudMpper.getPageAdvConmment(advCommentList.get(i).getAdvInfoId(), 0, 20, Integer.valueOf(advCommentList.get(i).getId()));
                if(advCommentListzi.size()>0){
                    List<AdvComment>  chihdeList= getListAdvComent(advCommentListzi);
                    advCommentList.get(i).setAdvCommentList(chihdeList);
                }
                    if(advCommentList.size()-1!=i){
                        continue;
                    }else{
                        break;
                    }
            }

        return  advCommentList;
    }

    @Override
    public JSONUtil getPageAdvList(String areaCode, String userId, String farmId, Integer pageNum, Integer pageSise, int type,String loginName,String advId,String expertUserId,String keyName) {
        JSONUtil jsonUtil=new JSONUtil();
        List<AdvList> listList=null;
        pageNum=(pageNum-1)*pageSise;
        int count=0;
        String ak=null;
        try {
            if(type==1){
                ak="3";
                //所有
            }
            if(type==2){
                //我的咨询列表
            }
            if(type==3){
                ak="1";
                //收藏列表

            }
//            if(type==3){
//                //点赞列表
//                listList=advisoryCloudMpper.getPageAdvList(areaCode,userId,farmId,pageNum,pageSise,"2");
//                count=advisoryCloudMpper.getPageAdvListCount(areaCode,userId,farmId,pageNum,pageSise,"2");
//            }
            listList=advisoryCloudMpper.getPageAdvList(areaCode,userId,farmId,pageNum,pageSise,ak,loginName,advId,expertUserId,keyName);
            count=advisoryCloudMpper.getPageAdvListCount(areaCode,userId,farmId,pageNum,pageSise,ak,loginName,advId,expertUserId,keyName);
            for (AdvList advList:listList){
                List<AdvComment> advCommentList = advisoryCloudMpper.
                        getPageAdvConmment(advList.getAdvId(),0,3,-1);
                advList.setAdvCommentList(getListAdvComent(advCommentList));
            }

        }catch (Exception e){
            e.printStackTrace();
            System.out.println("错错错"+e.getMessage());
            return   jsonUtil.setMsg("查询失败").setIsSuccess(false).setCode(100);

        }

        return jsonUtil.setData(listList).setCountSum(count);
    }

    @Override
    public String getAdvUserIdByParentId(String parentId) {
        return advisoryCloudMpper.getAdvUserIdByParentId(parentId);
    }

    @Override
    public String getAdvUserIdByAdvId(String advId) {
        return advisoryCloudMpper.getAdvUserIdByAdvId(advId);
    }

    @Override
    public List<String> getExpertUserIdByareacodeAndSubjectId(String areaCode, String SubjectId) {
        return advisoryCloudMpper.getExpertUserIdByareacodeAndSubjectId(areaCode,SubjectId);
    }

    @Override
    public Map<String, Object> getUserRoleByuserId(String userId) {
        return advisoryCloudMpper.getUserRoleByuserId(userId);
    }
}
